package ro.gateway.aida.locations;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import ro.gateway.aida.db.PersistenceManager;
import ro.gateway.aida.db.PersistenceToken;
import ro.gateway.aida.obj.CRegion;
import ro.gateway.aida.obj.Country;
import ro.gateway.aida.obj.County;
import ro.gateway.aida.obj.Region;
import ro.gateway.aida.obj.Town;
import ro.gateway.aida.obj.Village;
import ro.gateway.aida.obj.db.AIDALocationDB;
import ro.gateway.aida.obj.db.CountyDB;
import ro.gateway.aida.obj.db.RegionDB;
import ro.gateway.aida.obj.db.TownDB;
import ro.gateway.aida.obj.db.VillageDB;
import ro.gateway.aida.utils.HttpUtils;
import ro.gateway.aida.utils.Utils;


/**
 * <p>Title: Romanian AIDA</p> <p>Description: </p> <p>Copyright: Copyright (comparator) 2003</p> <p>Company:
 * ro-gateway</p>
 *
 * @author Mihai Popoaei, mihai_popoaei@yahoo.com, smike@intellisource.ro
 * @version 1.0-* @version $Id: EditLocationAction.java,v 1.1 2004/10/24 23:37:41 mihaipostelnicu Exp $
 */
public class EditLocationAction extends Action {
     public ActionForward execute( ActionMapping mapping,
                                   ActionForm form_,
                                   HttpServletRequest request,
                                   HttpServletResponse response )
        throws Exception {
          PersistenceToken token = PersistenceManager.
             tokenLookup( this.getServlet().getServletContext() );

          EditLocationForm form = ( EditLocationForm ) form_;
          form.clear();
          form.populate( request );
          LocationEditorForm main_form = ( LocationEditorForm ) request.getSession()
             .getAttribute( "loceditorform" );

          if ( "update".equals( form.action ) ) {
               // update location
               try {
                    int id = HttpUtils.getInt( form.id, -1 );
                    if ( "cregion".equals( form.otype ) ) {

                         CRegion item = new CRegion();
                         item.setId( id );
                         item.setName( form.name );
                         item.getGeo_data().populate( form.latitude, form.longitude, form.geo_code );
                         item.setCode( form.cregion_code );
                         item.setParent_id( form.cregion_parent_id );
                         AIDALocationDB.getManager( token ).updateCregion( item );
				} else if ( "country".equals( form.otype ) ) {
					Country item = new Country();
					item.setIso3( form.id );
					item.setName( form.name );
					item.getGeo_data().populate( form.latitude, form.longitude, form.geo_code );
					AIDALocationDB.getManager( token ).updateCountry( item );
				} else if ( "region".equals( form.otype ) ) {
					Region item = new Region();
					item.setCountryIso3( main_form.getCountry_iso3() );
					item.setId( id );
					item.setName( form.getName() );
					item.getGeo_data().populate( form.latitude, form.longitude, form.geo_code );
					RegionDB.getManager( token ).updateRegion( item );
				} else if ( "county".equals( form.otype ) ) {
					County item = new County();
					item.setId( id );
					item.setName( form.getName() );
					item.setRegiune_id( main_form.getRegion_id() );
					item.getGeo_data().populate( form.latitude, form.longitude, form.geo_code );
					CountyDB.getManager( token ).updateCounty( item );
				} else if ( "town".equals( form.otype ) ) {
					Town item = new Town();
					item.setName( form.name );
					item.setJudet_id( main_form.getCounty_id() );
					item.setSirsup_id( id );
					item.setType( ( byte ) form.type );
					item.getGeo_data().populate( form.latitude, form.longitude, form.geo_code );
					TownDB.getManager( token ).updateTown( item );
				} else if ( "village".equals( form.otype ) ) {
					Village item = new Village();
					item.setSiruta_id( id );
					item.setName( form.name );
					item.setSirsup_id( main_form.getTown_id() );
					item.getGeo_data().populate( form.latitude, form.longitude, form.geo_code );
					VillageDB.getManager( token ).updateVillage( item );
                    }
                    response.sendRedirect( "editor.do?" + main_form.getParametersForEditor() );
                    return null;
               } catch ( Exception ex ) {
                    ex.printStackTrace();
               }
          }
          if ( "create".equals( form.action ) ) {
               if ( "cregion".equals( form.otype ) ) {
                    request.setAttribute( "cregions", AIDALocationDB
                       .getManager( token ).getRootCregions( false ) );
               }

               if ( form.otype != null && form.name != null ) {
                    // insert location
                    try {
                         int id = HttpUtils.getInt( form.id, -1 );
                         if ( "cregion".equals( form.otype ) ) {
                              CRegion item = new CRegion();
                              item.setId( id );
                              item.setName( form.name );
                              item.getGeo_data().populate( form.latitude, form.longitude, form.geo_code );
                              item.setCode( form.cregion_code );
                              item.setParent_id( form.cregion_parent_id );
                              AIDALocationDB.getManager( token ).insertCRegion( item );
                         } else if ( "country".equals( form.otype ) ) {
                              Country item = new Country();
                              item.setIso3( form.id );
                              item.setName( form.name );
                              item.setCregion_id( main_form.getCregion_id() );
						item.getGeo_data().populate( form.latitude, form.longitude, form.geo_code );
                              AIDALocationDB.getManager( token ).insertCountry( item );
                         } else if ( "region".equals( form.otype ) ) {
                              Region item = new Region();
                              item.setCountryIso3( main_form.getCountry_iso3() );
                              item.setId( id );
                              item.setName( form.getName() );
						item.getGeo_data().populate( form.latitude, form.longitude, form.geo_code );
                              RegionDB.getManager( token ).insertRegion( item );
                         } else if ( "county".equals( form.otype ) ) {
                              County item = new County();
                              item.setId( id );
                              item.setName( form.getName() );
                              item.setRegiune_id( main_form.getRegion_id() );
						item.getGeo_data().populate( form.latitude, form.longitude, form.geo_code );
                              CountyDB.getManager( token ).insertCounty( item );
                         } else if ( "town".equals( form.otype ) ) {
                              Town item = new Town();
                              item.setName( form.name );
                              item.setJudet_id( main_form.getCounty_id() );
                              item.setSirsup_id( id );
                              item.setType( ( byte ) form.type );
						item.getGeo_data().populate( form.latitude, form.longitude, form.geo_code );
                              TownDB.getManager( token ).insertTown( item );
                         } else if ( "village".equals( form.otype ) ) {
                              Village item = new Village();
                              item.setSiruta_id( id );
                              item.setName( form.name );
                              item.setSirsup_id( main_form.getTown_id() );
						item.getGeo_data().populate( form.latitude, form.longitude, form.geo_code );
                              VillageDB.getManager( token ).insertVillage( item );
                         }

//                              System.out.println( "Am adaugat... ma intorc in main editor" );
                         response.sendRedirect( "editor.do?" + main_form.getParametersForEditor() );
                         return null;
                    } catch ( Exception ex ) {
                         ex.printStackTrace();
                    }
               }
          } else if ( "delete".equals( form.action ) ) {
               try {
                    int id = HttpUtils.getInt( form.id, -1 );
                    if ( "cregion".equals( form.otype ) ) {
                         AIDALocationDB.getManager( token ).deleteCRegion( id );
                         main_form.cregion = null;
                         main_form.cregion_id = -1;
                    } else if ( "country".equals( form.otype ) ) {
                         AIDALocationDB.getManager( token ).deleteCountry( form.id );
                         main_form.country = null;
                         main_form.country_iso3 = null;
                    } else if ( "region".equals( form.otype ) ) {
                         RegionDB.getManager( token ).deleteRegion( id );
                         main_form.region = null;
                         main_form.region_id = -1;
                    } else if ( "county".equals( form.otype ) ) {
                         CountyDB.getManager( token ).deleteCounty( id );
                         main_form.county = null;
                         main_form.county_id = -1;
                    } else if ( "town".equals( form.otype ) ) {
                         TownDB.getManager( token ).deleteTown( id );
                         main_form.town = null;
                         main_form.town_id = -1;
                    } else if ( "village".equals( form.otype ) ) {
                         VillageDB.getManager( token ).deleteVillage( id );
                         main_form.village = null;
                         main_form.village_id = -1;
                    }

//                        System.out.println( "Am sters... ma intorc in main editor" );
                    response.sendRedirect( "editor.do?" + main_form.getParametersForEditor() );
                    return null;
               } catch ( Exception ex ) {
                    ex.printStackTrace();
               }
          } else if ( "edit".equals( form.action ) ) {
               int id = HttpUtils.getInt( form.id, -1 );

			request.setAttribute( "cregions", AIDALocationDB
			   .getManager( token ).getRootCregions( false ) );
			form.action = "update";
               if ( "cregion".equals( form.otype ) ) {
                    CRegion region = AIDALocationDB.getManager( token )
                       .getCRegionById( id );
				if (region == null) {
					response.sendError(HttpServletResponse.SC_NOT_FOUND);
					return null;
				}
                    form.cregion_code = region.getCode();
                    form.name = region.getName();
                    form.geo_code = region.getGeo_data().getGeo_code();
                    form.latitude = region.getGeo_data().getLatitude();
                    form.longitude = region.getGeo_data().getLongitude();
                    form.cregion_parent_id = region.getParent_id();
			} else if ("country".equals(form.otype)) {
                    Country item = AIDALocationDB.getManager(token).getCountryByISO3(form.id);
				if (item == null) {
					response.sendError(HttpServletResponse.SC_NOT_FOUND);
					return null;
				}
				form.name = item.getName();
				form.latitude = item.getGeo_data().getLatitude();
				form.longitude = item.getGeo_data().getLongitude();
				form.geo_code = item.getGeo_data().getGeo_code();
			} else if ("region".equals(form.otype)) {
				Region item = RegionDB.getManager(token).getById(id);
				if (item == null) {
					response.sendError(HttpServletResponse.SC_NOT_FOUND);
					return null;
				}
				form.name = item.getName();
				form.latitude = item.getGeo_data().getLatitude();
				form.longitude = item.getGeo_data().getLongitude();
				form.geo_code = item.getGeo_data().getGeo_code();
			} else if ("county".equals(form.otype)) {
				County item = CountyDB.getManager(token).getById(id);
				if (item == null) {
					response.sendError(HttpServletResponse.SC_NOT_FOUND);
					return null;
				}
				form.name = item.getName();
				form.latitude = item.getGeo_data().getLatitude();
				form.longitude = item.getGeo_data().getLongitude();
				form.geo_code = item.getGeo_data().getGeo_code();
			} else if ("town".equals(form.otype)) {
				Town item = TownDB.getManager(token).getById(id);
				if (item == null) {
					response.sendError(HttpServletResponse.SC_NOT_FOUND);
					return null;
				}
				form.name = item.getName();
				form.latitude = item.getGeo_data().getLatitude();
				form.longitude = item.getGeo_data().getLongitude();
				form.geo_code = item.getGeo_data().getGeo_code();
				form.type = item.getType();
			} else if ("village".equals(form.otype)) {
				Village item = VillageDB.getManager(token).getById(id);
				if (item == null) {
					response.sendError(HttpServletResponse.SC_NOT_FOUND);
					return null;
				}
				form.name = item.getName();
				form.latitude = item.getGeo_data().getLatitude();
				form.longitude = item.getGeo_data().getLongitude();
				form.geo_code = item.getGeo_data().getGeo_code();
			}
          }
          Utils.setBreadCrumbs( request, "bc.locman" );
          return mapping.findForward( "jsp_page" );
     }
}
